package org.example.samplespringbootconsumer.client;

import org.junit.jupiter.api.Test;
import org.redisson.Redisson;
import org.redisson.api.*;
import org.redisson.api.listener.MapPutListener;
import org.redisson.api.listener.MapRemoveListener;
import org.redisson.api.map.event.*;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.concurrent.TimeUnit;

@SpringBootTest
public class rredis {

    @Test
    public void test(){
        RedissonClient redissonClient;
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://192.168.101.128:6379");
        redissonClient = Redisson.create(config);
        RMapCache<String, String> map = redissonClient.getMapCache("/rpc/com.heartunderblade.rpc.service.UserService:1.0");
        map.put("127.0.0.1:8000","1.0");
    }
    @Test
    public void eee() {
        // 配置 Redisson 客户端
        Config config = new Config();
        config.useSingleServer().setAddress("redis://192.168.101.128:6379");
        RedissonClient redissonClient = Redisson.create(config);

        // 获取 Redis 的发布/订阅频道
        RTopic topic = redissonClient.getTopic("__keyevent@0__:expired");  // 订阅过期事件

        // 监听过期事件
        topic.addListener(String.class, (channel, msg) -> {
            System.out.println("Key expired: " + msg); // 打印过期的键
            // 在这里你可以更新本地缓存或执行其他操作
        });

        // 订阅删除事件
        RTopic delTopic = redissonClient.getTopic("__keyevent@0__:del");
        delTopic.addListener(String.class, (channel, msg) -> {
            System.out.println("Key deleted: " + msg); // 打印删除的键
            // 在这里你可以更新本地缓存或执行其他操作
        });

        // 通过 Redisson 操作 Redis 数据
        redissonClient.getBucket("testKey").set("testValue");  // 设置一个键
        redissonClient.getBucket("testKey").delete();  // 删除该键

        // 关闭 Redisson 客户端连接
        redissonClient.shutdown();
    }
    @Test
    public void qqq() {
        // Configure Redisson client
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://192.168.101.128:6379");
        RedissonClient redissonClient = Redisson.create(config);

        // Get RMap instance from Redis
        RMap<String, String> map = redissonClient.getMap("/rpc/com.heartunderblade.rpc.service.UserService:1.0");

        // Add a listener for the PUT event (when a key-value pair is added)
        map.addListener(new MapPutListener() {
            @Override
            public void onPut(String s) {
                System.out.println("Key added: " + s);
            }
        });

// 添加监听器，用于监听 REMOVE 事件（当键值对被移除时）
        map.addListener(new MapRemoveListener() {
            @Override
            public void onRemove(String s) {
                System.out.println("Key removed: " + s);
            }


        });

        // Simulate adding and removing key-value pairs in the map
        map.put("127.0.0.1:8000", "1.0");  // This will trigger MapPutListener


        // Shutdown Redisson client
        redissonClient.shutdown();
    }
    @Test
    public void www(){
        // 配置 Redisson 客户端
        Config config = new Config();
        config.useSingleServer().setAddress("redis://192.168.101.128:6379");

        // 创建 Redisson 客户端
        RedissonClient redisson = Redisson.create(config);

        // 获取 RMapCache 实例
        RMapCache<String, Integer> map = redisson.getMapCache("/rpc/com.heartunderblade.rpc.service.UserService:1.0");

        // 添加监听器
        int updateListener = map.addListener(new EntryUpdatedListener<String, Integer>() {
            @Override
            public void onUpdated(EntryEvent<String, Integer> event) {
                System.out.println("Key updated: " + event.getKey());
                System.out.println("Old value: " + event.getOldValue());
                System.out.println("New value: " + event.getValue());
            }
        });

        int createListener = map.addListener(new EntryCreatedListener<String, Integer>() {
            @Override
            public void onCreated(EntryEvent<String, Integer> event) {
                System.out.println("Key created: " + event.getKey());
                System.out.println("Value: " + event.getValue());
            }
        });

        int expireListener = map.addListener(new EntryExpiredListener<String, Integer>() {
            @Override
            public void onExpired(EntryEvent<String, Integer> event) {
                System.out.println("Key expired: " + event.getKey());
                System.out.println("Value: " + event.getValue());
            }
        });

        int removeListener = map.addListener(new EntryRemovedListener<String, Integer>() {
            @Override
            public void onRemoved(EntryEvent<String, Integer> event) {
                System.out.println("Key removed: " + event.getKey());
                System.out.println("Value: " + event.getValue());
            }
        });

        // 修改 RMapCache 中的值以触发监听器
        map.put("key1", 100); // 触发 onCreate
        map.put("key1", 200); // 触发 onUpdate
        map.remove("key1");   // 触发 onRemove
        map.put("key2", 300, 5, TimeUnit.SECONDS); // 设置过期时间，触发 onCreate
        // 等待 key2 过期，触发 onExpired

        // 移除监听器
        map.removeListener(updateListener);
        map.removeListener(createListener);
        map.removeListener(expireListener);
        map.removeListener(removeListener);

        // 关闭 Redisson 客户端
        redisson.shutdown();

    }
}
